package com.example.clocktest

import android.content.Context
import androidx.compose.ui.platform.LocalContext
import okhttp3.Interceptor
import okhttp3.Response

class ReceivedCookiesInterceptor() : Interceptor {
    override fun intercept(chain: Interceptor.Chain): Response {
        val originalResponse = chain.proceed(chain.request())
        if (!originalResponse.headers("Set-Cookie").isEmpty()) {
            val cookies = originalResponse.headers("Set-Cookie").joinToString("; ")
            // 保存 Cookie 到本地存储，例如 SharedPreferences
            saveCookie(cookies)
        }
        return originalResponse
    }

    private fun saveCookie(cookies: String) {
        // 示例：保存到 SharedPreferences
        val sharedPreferences = MyApp.getContext().getSharedPreferences("cookie_prefs", Context.MODE_PRIVATE)
        sharedPreferences.edit().putString("cookie", cookies).apply()
    }
}